package com.qcby.byc.demo0422.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.qcby.byc.demo0422.entity.Person;
import com.qcby.byc.demo0422.mapper.PersonMapper;
import com.qcby.byc.demo0422.service.DepartService;
import com.qcby.byc.demo0422.service.PersonService;
import com.qcby.byc.demo0422.util.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @className: PersonServiceImpl
 * @description:
 * @author: lxt
 * @create: 2021-04-22 17:16
 **/

/**
 * 整个service实现类都添加上了事务
 *      1、查询不涉及到数据库的数据修改，单纯的查询方法不用事务
 *      2、添加、删除、修改会涉及数据库数据修改，添加事务
 */
@Transactional
@Service // alt + enter回车
public class PersonServiceImpl extends ServiceImpl<PersonMapper,Person>  implements PersonService {




    @Autowired
    private PersonMapper personMapper;


    @Autowired
    private DepartService departService;


    @Override
    public PageVo listAll(int pageNum, int pageSize) {
        // 执行分页
//        Page<Object> page = PageHelper.startPage(pageNum, pageSize);
        // 执行查询所有的人员
        List<Person> personList = personMapper.listAll();
        return new PageVo(null);
    }

    @Override
    public List<Person> listAll1() {
        return personMapper.listAll1();
    }



    @Override
    public void testTr(){
        try {
            Person person = new Person();
            person.setName("lxt0430");
            person.setDepartId(3L);
            person.setSex(0);
            personMapper.save(person);
            // 其他业务方法
            // 0不能作为被除数，异常
            int  c = 1/0;
        }catch (Exception e){
            // 插入部门
            departService.save();
            throw new RuntimeException("");
        }
    }

    @Override
    public void saveNew(Person person) {
        personMapper.saveNew(person);
    }

    @Override
    public void saveOld(Person person) {
        personMapper.save(person);
    }

    @Override
    public void insertBatch(List<Person> personList) {
        personMapper.insertBatch(personList);
    }

    @Override
    public void deleteByIds(List<Long> idList) {
        personMapper.deleteByIds(idList);
    }

    @Override
    public List<Person> listPersonNew(String name) {
        return personMapper.listPersonNew(name);
    }

    @Override
    public List<Person> listPersonNewM() {
        return personMapper.listPersonNewM();
    }
}
